package com.chat.test.unit.mapper;

import com.chat.data.MySqlSingle;
import com.chat.entity.User;
import com.chat.mapper.UserMapper;
import com.chat.util.EntityPrintUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;


@BenchmarkMode(Mode.AverageTime)
@State(Scope.Thread)
//@Fork(2)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 1)
@Measurement(iterations = 1)
public class TsetTestStressUserMapper {
    UserMapper userMapper;
    SqlSession sqlSession;

    @Setup
    @Before
    public void initData() throws IOException {
        sqlSession = MySqlSingle.getMySqlSingle().getSqlSessionFactory().openSession();
        userMapper = MySqlSingle.getMySqlSingle().getSqlSessionFactory().getConfiguration().getMapper(UserMapper.class,sqlSession);
    }

    @Benchmark
    @Test
    public void selectAll() throws IOException {


        List<User> list = userMapper.selectUsers();
        list.stream()
                .map(e->{
                    return EntityPrintUtils.toString(e);
                })
                .parallel();
//                .forEach(System.out::println);
    }

    @Benchmark
    @Test
    public void updateUser() throws IOException {

        User user = userMapper.selectUserById(13);
        user.setName("修改咯");
        user.setFileId(13);
        userMapper.updateUser(user);
        sqlSession.commit();
    }


}
